home *** CD-ROM | disk | FTP | other *** search
/ Hackers Handbook - Millenium Edition / Hackers Handbook.iso / files / hfiles / ftpcheck-0_31.perl < prev    next >
Encoding:
Text File  |  1998-11-04  |  3.1 KB  |  169 lines

  1. #!/usr/bin/perl
  2.  
  3. ########################################################
  4. # ftpcheck v0.31                      [November 2, 1998] 
  5. #          http://david.weekly.org/code
  6. #
  7. # by David Weekly <dew@cs.stanford.edu>
  8. #
  9. # Thanks to Shane Kerr for cleaning up the process code!
  10. #
  11. # This code is under the GPL. Use it freely. Enjoy.
  12. #        Debug. Enhance. Email me the patches!
  13. ########################################################
  14.  
  15. use Socket;
  16. use Net::FTP;
  17.  
  18. # timeouts in seconds for creating a socket and connecting
  19. my $MAX_SOCKET_TIME = 2;
  20. my $MAX_CONNECT_TIME = 3;
  21.  
  22. my $HELP=qq{Usage: ftpcheck [-h | --help] [-p processes] [-d | --debug] host};
  23.  
  24.  
  25. my @hosts;
  26.  
  27. # how many simultaneous processes are we allowed to use?
  28. my $MAX_PROCESSES=10;
  29. my $DEBUG=0;
  30.  
  31. while($_=shift){
  32.     if(/^--(.*)/){
  33.     $_=$1;
  34.     if(/help/){
  35.         print $HELP;
  36.         exit(0);
  37.     }
  38.     if(/debug/){
  39.         $DEBUG=1;
  40.     }
  41.     }    
  42.     elsif(/^-(.*)/){
  43.     $_=$1;
  44.     if(/^h/ or /^\?/){        
  45.         print $HELP;
  46.         exit(0);
  47.     }
  48.     if(/^p/){
  49.         $MAX_PROCESSES=shift;
  50.     }
  51.     if(/^d/){
  52.         $DEBUG=1;
  53.     }
  54.     }else{
  55.     push @hosts,$_;
  56.     }
  57. }
  58.  
  59. if(!$hosts[0]){
  60.     print $HELP;
  61.     exit(-1);
  62. }
  63.  
  64. my $host;
  65. $|=1;
  66. print "ftpcheck v0.31 by dave weekly <dew\@cs.stanford.edu>\n\n";
  67.  
  68.  
  69. # go through all of the hosts, replacing subnets with all contained IPs.
  70. for $host (@hosts){
  71.     $_=shift(@hosts);
  72.  
  73.     # scan a class C
  74.     if(/^([^.]+)\.([^.]+)\.([^.]+)$/){
  75.     my $i;
  76.     print "Expanding class C $_\n" if($DEBUG);
  77.     for($i=1;$i<255;$i++){
  78.         my $thost="$_.$i";
  79.         push @hosts,$thost;
  80.     }
  81.     }
  82.     else{
  83.     push @hosts,$_;
  84.     }
  85. }
  86.  
  87. my @pids;
  88. my $npids=0;
  89.  
  90. for $host (@hosts){
  91.     my $pid;
  92.     $pid=fork();
  93.     if($pid>0){
  94.     $npids++;
  95.     if($npids>=$MAX_PROCESSES){
  96.         for(1..($MAX_PROCESSES)){
  97.         $wait_ret=wait();
  98.         if($wait_ret>0){
  99.             $npids--;
  100.         }
  101.         }
  102.     }
  103.     next;
  104.     }elsif(undef $pid){
  105.     print "fork error\n" if ($DEBUG);
  106.     exit(0);
  107.     }else{
  108.     my($proto,$port,$sin,$ip);
  109.     print "Trying $host\n" if ($DEBUG);
  110.     $0="(checking $host)";
  111.  
  112.     # kill thread on timeout
  113.     local $SIG{'ALRM'} = sub { exit(0); };
  114.  
  115.     alarm $MAX_SOCKET_TIME;
  116.     $proto=getprotobyname('tcp');
  117.     $port=21;
  118.     $ip=inet_aton($host);
  119.     if(!$ip){
  120.         print "couldn't find host $host\n" if($DEBUG);
  121.         exit(0);
  122.     }
  123.     $sin=sockaddr_in($port,$ip);
  124.     socket(Sock, PF_INET, SOCK_STREAM, $proto);
  125.  
  126.     alarm $MAX_CONNECT_TIME;
  127.     if(!connect(Sock,$sin)){
  128.         exit(0);
  129.     }
  130.     my $iaddr=(unpack_sockaddr_in(getpeername(Sock)))[1];
  131.     close(Sock);       
  132.     
  133.     # SOMETHING is listening on the FTP port...really ftp server?
  134.     
  135.     print "listen $host!\n" if($DEBUG);
  136.     alarm 0;
  137.     $hostname=gethostbyaddr($iaddr,AF_INET);
  138.  
  139.     # create new FTP connection w/5 second timeout
  140.     $ftp = Net::FTP->new($host, Timeout =>  5);
  141.     if(!$ftp){
  142.         print "      <$host ($hostname) denied you>\n" if($DEBUG);
  143.         exit(0);
  144.     }
  145.     if(!$ftp->login("anonymous","just\@checking.com")){
  146.         print "   FTP on $host [$hostname]\n";
  147.         exit(0);
  148.     }
  149.  
  150.     print "Anon FTP on $host [$hostname]\n";
  151.  
  152.     $ftp->quit;
  153.     exit(0);
  154.     }
  155. }
  156.  
  157. print "done spawning, $npids children remain\n" if($DEBUG);
  158.  
  159. # wait for my children
  160. for(1..$npids){
  161.     my $wt=wait();
  162.     if($wt==-1){
  163.     print "hey $!\n" if($DEBUG);
  164.     redo;
  165.     }
  166. }
  167.  
  168. print "Done\n";
  169.